import re

import tqdm
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from ai.utils import utils_spider, utils_file


def get_audio_name(title: str):
    pattern = r'《(.*?)》'  # 匹配《和》之间的任意字符（非贪婪匹配）
    matches = re.findall(pattern, title)

    if matches:
        return f"《{matches[0]}》"
    else:
        return title


def main():
    res_dic = {}
    url = "https://www.dy2018.com/html/tv/hytv/index_105.html"
    html_text, driver = utils_spider.get_source_page_from_url(url, )
    # print(response.text)
    a_xpath = '/html/body/div[1]/div/div[3]/div[6]/div[2]/div[2]/div[2]/ul/table/tbody/tr[2]/td[2]/b/a/@href'
    a_title_xpath = '/html/body/div[1]/div/div[3]/div[6]/div[2]/div[2]/div[2]/ul/table/tbody/tr[2]/td[2]/b/a/@title'
    res = utils_spider.handle_xpath(html_text, a_xpath)
    res_title = utils_spider.handle_xpath(html_text, a_title_xpath)
    if len(res) != len(res_title):
        print("title href长度不匹配")
    for title, href in zip(res_title, res):
        res_dic[get_audio_name(title)] = "https://www.dy2018.com" + href
    utils_file.write_dict_to_json(res_dic, "./output/res13.json")


def get_url_for_audio_page():
    """
    电影天堂
    """
    res_dic = {}
    for i in tqdm.tqdm(range(1, 106), total=105):
        if i == 1:
            url = "https://www.dy2018.com/html/tv/hytv/index.html"
            html_text, driver = utils_spider.get_source_page_from_url(url, )
        else:
            url = f"https://www.dy2018.com/html/tv/hytv/index_{i}.html"
            html_text, driver = utils_spider.get_source_page_from_url_have_driver(url, driver)
        # print(response.text)
        a_xpath = '/html/body/div[1]/div/div[3]/div[6]/div[2]/div[2]/div[2]/ul/table/tbody/tr[2]/td[2]/b/a/@href'
        a_title_xpath = '/html/body/div[1]/div/div[3]/div[6]/div[2]/div[2]/div[2]/ul/table/tbody/tr[2]/td[2]/b/a/@title'
        res = utils_spider.handle_xpath(html_text, a_xpath)
        res_title = utils_spider.handle_xpath(html_text, a_title_xpath)
        if len(res) != len(res_title):
            print("title href长度不匹配")
        for title, href in zip(res_title, res):
            res_dic[get_audio_name(title)] = "https://www.dy2018.com" + href
    utils_file.write_dict_to_json(res_dic, "./output/res_gxl.json")


def get_url_for_audio_page2():
    """
    bt天堂
    """
    res_dic = {}
    options = Options()
    options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome(options=options)
    for i in tqdm.tqdm(range(0, 127), total=126):
        url = f"https://www.bt-tt.com/html/9-{i}.html"
        html_text, driver = utils_spider.get_source_page_from_url_have_driver(url, driver)
        a_xpath = '/html/body/div[4]/div/div/div[2]/div/ul/li/div[2]/span/a/@href'
        a_title_xpath = '/html/body/div[4]/div/div/div[2]/div/ul/li/div[2]/strong/h3/a/strong/font/text()'
        res = utils_spider.handle_xpath(html_text, a_xpath)
        print(res)
        res_title = utils_spider.handle_xpath(html_text, a_title_xpath)
        if len(res) != len(res_title):
            print("title href长度不匹配")
        for title, href in zip(res_title, res):
            res_dic[get_audio_name(title)] = "https://www.bt-tt.com" + href
    utils_file.write_dict_to_json(res_dic, "./output/res_gxl_bt_tiantang.json")


def handle_one_audio_page(a_xpath):
    driver = webdriver.Chrome()
    big_res_dict = {}
    res_dic = utils_file.load_json_file("./output/res_gxl_2.json")
    index = 0
    total = len(res_dic)
    try:
        for k, v in res_dic.items():
            index += 1
            print(f'handle{index}/{total}:{k}...ing, url:{v}')
            html_text, _ = utils_spider.get_source_page_from_url_have_driver(v, driver)
            a_xpath = '/html/body/div[1]/div/div[3]/div/div[6]/div[2]/ul/div[2]/div[2]/table/tbody/tr/td/a/@href'
            a_xpath = '/html/body/div[1]/div/div[3]/div/div[6]/div[2]/ul/div[2]/div[2]/table/tbody/tr/td/anchor/a/@thunderhref'
            a_xpath = '//*[@id="downlist"]/table/tbody/tr/td/a/@href'
            a_xpath = a_xpath
            res = utils_spider.handle_xpath(html_text, a_xpath)
            print('res:', res)
            big_res_dict[k] = {'page_url': v, 'audio_url': res}
    except Exception as e:
        print(e)
    finally:
        utils_file.write_dict_to_json(big_res_dict, "./output/res_gxl_big.json")
    utils_file.write_dict_to_json(big_res_dict, "./output/res_gxl_big.json")


def handle_one_audio_page2():
    options = Options()
    options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    driver = webdriver.Chrome(options=options)
    big_res_dict = {}
    res_dic = utils_file.load_json_file("./output/res_gxl_bt_tiantang.json")
    index = 0
    total = len(res_dic)
    try:
        for k, v in res_dic.items():
            index += 1
            print(f'handle{index}/{total}:{k}...ing, url:{v}')
            html_text, _ = utils_spider.get_source_page_from_url_have_driver(v, driver)
            # a_xpath='//*[@id="pad_01"]/div/div/div/div[2]/div[3]/p[2]/a/@href'
            a_xpath = '//*[@id="pad_01"]/div/div/div/div[2]/div[3]/p[2]/a/@href'
            ress = utils_spider.handle_xpath(html_text, a_xpath)
            title_xpath = '//*[@id="pad_01"]/div/div/div/div[2]/div[3]/p[2]/a/text()'
            titles = utils_spider.handle_xpath(html_text, title_xpath)
            temp_dict = {}
            for title, url in zip(titles, ress):
                temp_dict[title] = url
            print(temp_dict)
            big_res_dict[k] = {'page_url': v, 'audio_url': temp_dict}
    except Exception as e:
        print(e)
    finally:
        utils_file.write_dict_to_json(big_res_dict, "./output/res_gxl_big_bt_taintang.json")
    utils_file.write_dict_to_json(big_res_dict, "./output/res_gxl_big_bt_taintang.json")


def handle_none():
    res_dic = {}
    dic_big = utils_file.load_json_file("./output/res_gxl_big.json")
    for k, v in dic_big.items():
        if v['audio_url'] is None:
            print(k, v)
            res_dic[k] = v['page_url']
    utils_file.write_dict_to_json(res_dic, './output/res_gxl_2.json')
    print(len(res_dic))


def handle_res_to_get_list():
    res_list = []
    res_dic = utils_file.load_json_file("./output/res_gxl_source.json")
    for k, v in res_dic.items():
        if v['audio_url'] is not None:
            list_audio = v['audio_url']
            for i in list_audio:
                res_list.append(i)
    print(len(res_list))
    print(res_list)
    # utils_file.write_list_to_file(res_list, './output/list.txt')
    with open('./output/list.txt', 'w', encoding='utf=8') as f:
        for res in res_list:
            f.write(res + '\n')

def handle_res_to_get_list2():
    res_list = []
    res_dic = utils_file.load_json_file("./output/res_gxl_big_bt_taintang.json")
    for k, v in res_dic.items():
        if v['audio_url'] is not None:
            dict_audio = v['audio_url']
            for k, v in dict_audio.items():
                res_list.append(v)
    print(len(res_list))
    print(res_list)
    # utils_file.write_list_to_file(res_list, './output/list_bt_tiantang.txt')
    with open('./output/list_bt_tiantang.txt', 'w', encoding='utf=8') as f:
        for res in res_list:
            f.write(res + '\n')


if __name__ == '__main__':
    # get_url_for_audio_page()
    # handle_one_audio_page()
    # handle_res_to_get_list()
    # get_url_for_audio_page2()
    # handle_one_audio_page2()
    handle_res_to_get_list2()